package com.rex.saas.controller;

import com.rex.saas.bean.BaseResponse;
import com.rex.saas.bean.ResultCode;
import com.rex.saas.bean.request.GroupAddDeviceRequest;
import com.rex.saas.bean.request.LabelAddRequest;
import com.rex.saas.bean.request.LabelDeleteRequest;
import com.rex.saas.bean.response.DeviceLabelResponse;
import com.rex.saas.bean.response.LabelResponse;
import com.rex.saas.constants.Constants;
import com.rex.saas.db.entity.*;
import com.rex.saas.service.DeviceService;
import com.rex.saas.service.GroupService;
import com.rex.saas.service.LabelService;
import com.rex.saas.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/list")
    public BaseResponse list(HttpServletRequest request, Long spaceId){
        log.info("list spaceId:{}", spaceId);
        return BaseResponse.ok(groupService.list(spaceId));
    }
    @GetMapping("/devices")
    public BaseResponse devices(HttpServletRequest request, @RequestParam Long groupId){
        log.info("devices groupId:{}", groupId);
        String token = request.getHeader("token");
        String tenantId = JwtUtils.getTenantId(token);
        SaasDeviceGroup group = groupService.getGroupById(groupId);
        if(group==null){
            log.error("devices groupId is null, groupId:{}", groupId);
            return BaseResponse.error("组不存在", ResultCode.PARAM_ERROR);
        }
        if(!group.getTenantId().equals(tenantId)){
            log.error("devices groupId customerId not match error, groupId:{}", groupId);
            return BaseResponse.error("非法组", ResultCode.PARAM_ERROR);
        }

        return BaseResponse.ok(groupService.queryDevices(groupId, token));
    }
    @PostMapping("/addDevice")
    public BaseResponse addDevice(HttpServletRequest request,  @RequestBody GroupAddDeviceRequest groupAddDeviceRequest){
        log.info("addDevice groupAddDeviceRequest :{}", groupAddDeviceRequest);
        String token = request.getHeader("token");
        String tenantId = JwtUtils.getTenantId(token);
        if(StringUtils.isEmpty(groupAddDeviceRequest.getDeviceName())){
            log.error("addDevice deviceName is null, groupAddDeviceRequest:{}", groupAddDeviceRequest);
            return BaseResponse.error("设备名称不能为空", ResultCode.PARAM_ERROR);
        }
        if(groupAddDeviceRequest.getGroupId()==null){
            log.error("addDevice groupId is null, groupAddDeviceRequest:{}", groupAddDeviceRequest);
            return BaseResponse.error("组不能为空", ResultCode.PARAM_ERROR);
        }
        SaasDeviceGroup group = groupService.getGroupById(groupAddDeviceRequest.getGroupId());
        if(group==null){
            log.error("addDevice groupId is null, groupAddDeviceRequest:{}", groupAddDeviceRequest);
            return BaseResponse.error("组不存在", ResultCode.PARAM_ERROR);
        }
        SaasDeviceInfo deviceInfo = deviceService.getDeviceByName(groupAddDeviceRequest.getDeviceName(), tenantId);
        if(deviceInfo==null){
            log.error("addDevice getDeviceByName error, deviceName:{}", groupAddDeviceRequest.getDeviceName());
            return BaseResponse.error("设备不存在", ResultCode.PARAM_ERROR);
        }

        return BaseResponse.ok(groupService.addDevice(groupAddDeviceRequest.getGroupId(), groupAddDeviceRequest.getDeviceName()));
    }
    @GetMapping("/deleteDevice")
    public BaseResponse addDevice(HttpServletRequest request,  @RequestParam Long id){
        log.info("deleteDevice id:{}", id);
        SaasDeviceGroupRelation relation = groupService.getDeviceRelationById(id);
        if(relation==null){
            log.error("deleteDevice relation is null, id:{}", id);
            return BaseResponse.error("设备不存在", ResultCode.PARAM_ERROR);
        }
        String deviceName = relation.getDeviceName();
        String tenantId = JwtUtils.getTenantId(request.getHeader("token"));
        SaasDeviceInfo deviceInfo = deviceService.getDeviceByName(deviceName, tenantId);
        if(deviceInfo==null){
            log.error("deleteDevice getDeviceByName error, id:{}", id);
            return BaseResponse.error("非法设备", ResultCode.PARAM_ERROR);
        }
        return BaseResponse.ok(groupService.deleteDevice(id));
    }
}

